package cn.bblocks.cacheTest;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import com.alibaba.fastjson.parser.ParserConfig;
//import com.blinkfox.fenix.jpa.FenixJpaRepositoryFactoryBean;

import cn.bblocks.cacheTest.model.User;
import cn.bblocks.cacheTest.model.UserBank;
import cn.bblocks.cacheTest.service.UserBankService;
import cn.bblocks.cacheTest.service.UserService;
import cn.bblocks.cacheTest.service.impl.AreaServiceImpl;
import cn.bblocks.cacheTest.suport.RandomValue;
import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@SpringBootApplication
@EnableAspectJAutoProxy(exposeProxy = true)
@EnableJpaAuditing
@EnableScheduling
//@EnableCaching

//@EnableJpaRepositories(repositoryFactoryBeanClass = FenixJpaRepositoryFactoryBean.class)
//@EnableAsync //开启异步调用
//@ComponentScan({"com.slyak","com.luxcon.activityCenter"})
//@EnableJpaRepositories(basePackages = "com.luxcon.activityCenter", repositoryBaseClass = GenericJpaRepositoryImpl.class, repositoryFactoryBeanClass = GenericJpaRepositoryFactoryBean.class)
@ComponentScan(basePackages={"cn.bblocks.cacheTest"})
public class CacheTestApplication extends SpringBootServletInitializer implements CommandLineRunner{
	
	@Autowired
    private ApplicationContext appContext;
	
	public static void main(String[] args) {
		ParserConfig.getGlobalInstance().addAccept("org.springframework.data.redis.LinkObject"); 
		ParserConfig.getGlobalInstance().setAutoTypeSupport(true); 
		SpringApplication.run(CacheTestApplication.class, args);
	}

	@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(CacheTestApplication.class);
    }
	
	 @Override
	 public void run(String... args) throws Exception
	  {
	        String[] beans = appContext.getBeanDefinitionNames();
	        //Arrays.sort(beans);
	        for (String bean : beans)
	        {
	            //System.out.println(bean + " of Type :: " + appContext.getBean(bean).getClass());
	        }
	        
	        
	        initUsers();
	        
	        System.out.println("load over~~");
	        
//	        initUsersBanks();

	   }
	
	
	 
	 
	 public void initUsers() {
		 
		 int total = 50000;
		 
		 UserService userService = appContext.getBean(UserService.class);
		 
		 
		 log.info("用户数量:{}",userService.totalUsers());
		 
		 if(userService.totalUsers() !=null && userService.totalUsers()>=total) {
			 return;
		 }
		 
		 List<User> users = new ArrayList();
		 for(int i=0;i<total;i++) {
			 userService.initUser(i);
		 }
		 
//		 if(users.isEmpty()) {
//			 userService.addUsers(users); 
//		 }
		 
	 }
	 
	

}
